#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;

const int MAXN = 2e5+10;
int prime[MAXN],v[MAXN];    //表示 i 的最小质因数。如果i就是质数，那么v[i] = i
int m=0;//m表示现在筛出m个质数
void primes(int n)
{
    for(int i=2;i<=n;i++)
    {
        if(v[i]==0)        //如果v[i]为0，说明 i 之前没有被筛到过，i 为质数
        {
            v[i] = i;
            prime[++m] = i;
        }
        for(int j = 1;j<=m;j++)    //遍历小于 i 的所有质数
        {
            //如果质数大于 i 的最小质因数,或者乘起来大于n就跳出循环
            if(prime[j] > v[i] || prime[j] > n/i) break;
            v[i*prime[j]] = prime[j];    //标记 i*prime[j] 的最小质因数是prime[j]
        }
    }
}
int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    int T; cin >>T;
    primes(MAXN-1);
    while(T--){
        int N; cin >> N;
        vector<bool> done(N+1,false);
        vector<int> a, b, P;
        for (int i = 1; i <= m; ++i) {
            if(prime[i] <= N/2)
                P.push_back(prime[i]);
            else break;
        }
        vector<int> two;
        for (int i = P.size()-1; i >=1 ; --i) {
            int p = P[i];
            vector<int> can;
            int t = p;
            while(t <= N) {
                if(!done[t])
                    can.push_back(t);
                t+=p;
            }
            if(can.size()>1){
                if(can.size()%2 == 0){
                    for (int j = 0; j < can.size(); j+=2) {
                        a.push_back(can[j]);
                        b.push_back(can[j+1]);
                        done[can[j]] = done[can[j+1]] = true;
                    }
                }else{
                    a.push_back(can[0]);
                    b.push_back(can[2]);
//                    two.push_back(can[1]);
                    done[can[0]] = done[can[2]] = true;
                    for (int j = 3; j < can.size(); j+=2) {
                        a.push_back(can[j]);
                        b.push_back(can[j+1]);
                        done[can[j]] = done[can[j+1]] = true;
                    }
                }
            }
        }
//        vector<int> last;
//        for (int i = 0; i < two.size(); ++i) {
//            if(!done[two[i]]) last.push_back(two[i]);
//        }
        for (int i = 2; i <= N; i+=2) {
            if(!done[i]) two.push_back(i);
        }
        for(int i=1; i<two.size(); i+=2){
            a.push_back(two[i-1]);
            b.push_back(two[i]);
            done[two[i]] = done[two[i-1]] = true;
        }
        cout << a.size()<<"\n";
        assert(a.size() == b.size());
        for (int i = 0; i < a.size(); ++i) {
            cout << a[i] << " " << b[i] << endl;
        }

    }
    return 0;
}